import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonArray;
import org.springframework.util.StringUtils;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;

public class ExcelToJsonConverter {
    public static void main(String[] args) {
        String excelFilePath = "/Users/piangpiang/Downloads/各类数据内置模型.xlsx";

        try (FileInputStream fis = new FileInputStream(excelFilePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            Sheet sheet = workbook.getSheetAt(0);
            Iterator<Row> iterator = sheet.iterator();

            Map<String, JsonObject> modelMap = new LinkedHashMap<>();

            // 跳过表头
            if (iterator.hasNext()) {
                iterator.next();
            }

            // 处理每一行
            String model = "";
            while (iterator.hasNext()) {
                Row currentRow = iterator.next();
                String modelCur = currentRow.getCell(0).getStringCellValue().trim();
                if(!StringUtils.isEmpty(modelCur)){
                    if("日志类内置模型".equals(modelCur)){
                        model="LOG";
                    }else if("指标类内置模型".equals(modelCur)){
                        model="METRIC";
                    }else if("告警类内置模型".equals(modelCur)){
                        model="ALARM";
                    }else if("调用链内置模型".equals(modelCur)){
                        model="TRACE";
                    }else {
                        model="OTHER";
                    }
                }
                
                String name = currentRow.getCell(1).getStringCellValue().trim();
                if (StringUtils.isEmpty(name)) {
                    continue;
                }
                if("LOG".equals(model)){
                    if("_raw".equals(name)){
                        name = "data";
                    }else {
                        name = "dataTags."+name;
                    }
                }
                String alias = currentRow.getCell(2).getStringCellValue().trim();
                String valueType = currentRow.getCell(4).getStringCellValue().trim();
                String isNotNull = currentRow.getCell(5).getStringCellValue().trim();
                String fillType = currentRow.getCell(6).getStringCellValue().trim();

                JsonObject nameObject = new JsonObject();
                nameObject.addProperty("name", name);
                nameObject.addProperty("alias", alias);
                if (valueType.equals("长整型")) {
                    valueType = "Long";
                } else if (valueType.equals("整形")) {
                    valueType = "Integer";
                } else if (valueType.equals("布尔")) {
                    valueType = "Boolean";
                } else if (valueType.equals("浮点")) {
                    valueType = "Float";
                } else {
                    valueType = "String";
                }
                nameObject.addProperty("valueType", valueType);
                nameObject.addProperty("isNotNull", isNotNull.equals("非空") ? 0 : 1);
                nameObject.addProperty("fillType", fillType.equals("人工解析") ? 1 : 0);
                nameObject.addProperty("capture", true);
                nameObject.addProperty("text", "");
                nameObject.addProperty("valuePreview", "");
                nameObject.addProperty("type", 0);
                nameObject.addProperty("firstLayer", false);
                nameObject.addProperty("originIndex", 0);
                if("LOG".equals(model)){
                    if("data".equals(name)){
                        nameObject.addProperty("modelField", "_raw");
                    }else{
                        nameObject.addProperty("modelField", name.replaceFirst("dataTags.",""));
                    }
                }else{
                    nameObject.addProperty("modelField", name);
                }
                
                modelMap.computeIfAbsent(model, k -> new JsonObject()).add(name, nameObject);
            }

            JsonObject finalJson = new JsonObject();

            for (Map.Entry<String, JsonObject> entry : modelMap.entrySet()) {
                finalJson.add(entry.getKey(), entry.getValue());
            }

            System.out.println(finalJson.toString());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}